package citylife.geotools.demo;

import java.io.File;

import org.geotools.data.DataUtilities;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.collection.SpatialIndexFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;

public class Class1QuickStart {

  /**
   * GeoTools Quickstart demo application. Prompts the user for a shapefile and displays its
   * contents on the screen in a map frame
   */
  public static void main(String[] args) throws Exception {
      // display a data store file chooser dialog for shapefiles
      String path = "shp";
//      String path = "D:\\文档\\GIS\\三阶段-质检\\二维数据文件\\50m_cultural";
      File file = JFileDataStoreChooser.showOpenFile(path, null);
      if (file == null) {
          return;
      }

      FileDataStore store = FileDataStoreFinder.getDataStore(file);
      // 未读入内存的featureSource
      SimpleFeatureSource featureSource = store.getFeatureSource();

      // Create a map content and add our shapefile to it
      MapContent map = new MapContent();
      map.setTitle("Quickstart Using cached features");

      Style style = SLD.createSimpleStyle(featureSource.getSchema());
      
      // 读入内存的cachedSource
      SimpleFeatureSource cachedSource =
          DataUtilities.source(
              new SpatialIndexFeatureCollection(featureSource.getFeatures()));
      
      Layer layer = new FeatureLayer(cachedSource, style);
      map.addLayer(layer);

      // Now display the map
      JMapFrame.showMap(map);
  }
  

}
